const Service = require('egg').Service;
const { Op } = require('sequelize');

class TaskService extends Service {
  async findAll(payload) {
    const { ctx } = this;
    const {
      limit,
      offset,
      prop_order,
      order,
      name,
      executor_ids,
      creator_ids,
      task_priority_ids,
      task_state_ids,
      date_start_created,
      date_end_created,
      participator_id,
    } = payload;
    const where = payload.where;
    const Order = [];
    !where[Op.or] ? (where[Op.or] = []) : null;
    executor_ids ? where[Op.or].push({ executor_id: executor_ids }) : null;
    creator_ids ? where[Op.or].push({ creator_id: creator_ids }) : null;
    task_priority_ids ? where[Op.or].push({ task_priority_id: task_priority_ids }) : null;
    task_state_ids ? where[Op.or].push({ task_state_id: task_state_ids }) : null;
    date_start_created || date_end_created
      ? (where[Op.and] = [
        {
          created_at: {
            [Op.gte]: date_start_created || '0001-01-01 00:00:01',
            [Op.lte]: date_end_created || '9001-01-01 00:00:01',
          },
        },
      ])
      : null;
    if (!where[Op.or].length) {
      delete where[Op.or];
    }
    name ? (where.name = { [Op.like]: `%${name}%` }) : null;
    const participatorsWhere = participator_id ? { id: participator_id } : null;
    prop_order && order ? Order.push([prop_order, order]) : null;
    let whereProject = null;
    if (!limit) {
      whereProject = {};
      // 只有项目成员才能查看项目任务，除非是公开项目
      whereProject[Op.or] = [
        { id: ctx.currentRequestData.userInfo.id },
        {
          '$project.is_private$': 0,
        },
      ];
    }
    return await ctx.model.Tasks.findAndCountAll({
      distinct: true,
      limit,
      offset,
      where,
      order: Order,
      include: [
        // {
        //   model: ctx.model.TaskTags,
        //   through: { attributes: [] },
        //   // attributes: ['username', 'id', 'avatar'],
        // },
        {
          model: ctx.model.Users,
          attributes: ['username', 'id', 'avatar'],
          as: 'creator',
        },
        {
          model: ctx.model.Users,
          attributes: ['username', 'id', 'avatar'],
          as: 'executor',
        },
        {
          model: ctx.model.Users,
          as: 'participators',
          attributes: ['id', 'username', 'avatar'],
          through: { attributes: [] },
          where: participatorsWhere,
        },
        {
          model: ctx.model.Projects,
          as: 'project',
          required: true,
          attributes: ['id', 'name'],
          include: [
            {
              model: ctx.model.Users,
              as: 'member',
              attributes: ['id', 'username'],
              through: { attributes: [] },
              where: whereProject,
              required: true,
            },
          ],
        },
        // {
        //   model: ctx.model.Users,
        //   as: 'likers',
        //   attributes: ['id', 'username', 'avatar'],
        //   through: { attributes: [] },
        // },
      ],
    });
  }
}

module.exports = TaskService;